package com.zhan.ielts.dao.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import com.zhan.frame.core.dao.impl.BaseDaoImpl;
import com.zhan.frame.core.exception.DaoException;
import com.zhan.frame.core.utils.BeanUtils;
import com.zhan.ielts.dao.UnknownVocabularyDao;
import com.zhan.ielts.domain.UnknownVocabulary;

@Repository
public class UnknownVocabularyDaoImpl extends BaseDaoImpl<UnknownVocabulary> implements UnknownVocabularyDao {

	@Override
	public List<String> selectWrods(UnknownVocabulary entity) {
		return sqlSessionTemplate.selectList(getSqlName("selectWords"), entity);
	}

	@Override
	public void updateByCustom(List<UnknownVocabulary> entityList) {
		if (entityList == null || entityList.isEmpty())
			return;
		for (UnknownVocabulary entity : entityList) {
			sqlSessionTemplate.update(getSqlName("updateByCustom"), entity);
		}
	}

	@Override
	public List<UnknownVocabulary> selectLatest(UnknownVocabulary entity) {

		// return sqlSessionTemplate.update(getSqlName("updateByFields"),
		// entity);
		return sqlSessionTemplate.selectList(getSqlName("selectLatestOnes"), entity);

	}

	@Override
	public Page<UnknownVocabulary> selectLatestNew(Map<String, String> map, Pageable pageable) {
		try {
			Map<String, Object> param = BeanUtils.toMap(map, getRowBounds(pageable));
			param.putAll(map);
			List<UnknownVocabulary> contentList = sqlSessionTemplate.selectList(getSqlName("selectLatest"), param);
			return new PageImpl<UnknownVocabulary>(contentList, pageable, 50L);
		} catch (Exception e) {
			throw new DaoException(String.format("根据分页对象查询列表出错！语句:%s", getSqlName("selectLatest")), e);
		}

	}

	@Override
	public Long getCountByCutom(UnknownVocabulary entity) {
		return sqlSessionTemplate.selectOne(getSqlName("selectLatestCount"), entity);

	}

	@Override
	public int saveOrUpdate(List<UnknownVocabulary> entityList) {
		try {
			if (entityList != null && !entityList.isEmpty()) {
				for (UnknownVocabulary entity : entityList) {
					if (StringUtils.isBlank(entity.getId()))
						entity.setId(generateId());
					sqlSessionTemplate.insert(getSqlName("saveOrUpdate"), entity);
				}
			}
		} catch (Exception e) {
			throw new DaoException(String.format("添加对象出错！语句：%s", getSqlName("saveOrUpdate")), e);
		}
		return 0;
	}

}
